package com.yu.footballpromotionliveadmin.dao.impl;

import com.yu.footballpromotionliveadmin.dao.LiveBroadcasAdministratorDao;
import com.yu.footballpromotionliveadmin.mapper.LiveBroadcasAdministratorMapper;
import com.yu.footballpromotionliveadmin.page.PageInfo;
import com.yu.footballpromotionliveadmin.pojo.LiveBroadcasAdministrator;
import com.yu.footballpromotionliveadmin.util.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository
public class LiveBroadcasAdministratorDaoImpl implements LiveBroadcasAdministratorDao {

    @Resource
    LiveBroadcasAdministratorMapper liveBroadcasAdministratorMapper;

    @Override
    public List<LiveBroadcasAdministrator> getAllLiveBroadcasAdministrators() throws Exception {
        return liveBroadcasAdministratorMapper.selectByExample(null);
    }

    @Override
    public LiveBroadcasAdministrator getLiveBroadcasAdministratorByUno(String lbano) throws Exception {
        return liveBroadcasAdministratorMapper.selectByPrimaryKey(lbano);
    }

    @Override
    public List<LiveBroadcasAdministrator> getLiveBroadcasAdministratorsByPage(PageInfo pageInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) liveBroadcasAdministratorMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return liveBroadcasAdministratorMapper.getLiveBroadcasAdministratorByPage(map);
    }

    @Override
    public Integer deleteLiveBroadcasAdministrator(String lbano) throws Exception {
        return liveBroadcasAdministratorMapper.deleteByKey(lbano);
    }

    @Override
    public Integer updateLiveBroadcasAdministrator(LiveBroadcasAdministrator liveBroadcasAdministrator) throws Exception {
        return liveBroadcasAdministratorMapper.updateByPrimaryKey(liveBroadcasAdministrator);
    }

    @Override
    public Integer addLiveBroadcasAdministratorByUno(LiveBroadcasAdministrator liveBroadcasAdministrator) throws Exception {
                liveBroadcasAdministrator.setLbano(initId());
        return liveBroadcasAdministratorMapper.insert(liveBroadcasAdministrator);
    }

    @Override
    public LiveBroadcasAdministrator getLiveBroadcasAdministratorsWithOrders(String lbano) throws Exception {
        return null;
    }

    @Override
    public LiveBroadcasAdministrator getAccountByAccountName(String lbano) throws Exception {
        return null;
    }

    public String initId() {
       String maxUno = liveBroadcasAdministratorMapper.getMaxUno();
        maxUno =  Optional.ofNullable(maxUno).orElse("L0000");
        return GenNumTools.initId("L", 4, maxUno);
   }
}
